package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Inventory;
import com.zyd.shiro.business.service.PakInventoryService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakInventory;
import com.zyd.shiro.persistence.mapper.PakInventoryMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakInventoryServiceImpl implements PakInventoryService {

    @Autowired
    private PakInventoryMapper pakInventoryMapper;

    @Override
    public Inventory insert(Inventory inventory) {
        Assert.notNull(inventory, "对象不可为空！");
        PakInventory pakInventory = inventory.getPakInventory();
        pakInventory.setUnicode("PD"+System.currentTimeMillis());
        pakInventory.setStatus(1);
        pakInventory.setUpdateTime(new Date());
        pakInventory.setCreateTime(new Date());
        pakInventoryMapper.insertSelective(pakInventory);
        return inventory;
    }

    @Override
    public void insertList(List<Inventory> inventorys) {
            for(Inventory inventory:inventorys){
               inventory.getPakInventory().setUpdateTime(new Date());
               inventory.getPakInventory().setCreateTime(new Date());
               pakInventoryMapper.insertSelective(inventory.getPakInventory());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakInventoryMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Inventory inventory) {
        Assert.notNull(inventory, "对象不可为空！");
        inventory.setUpdateTime(new Date());
        return pakInventoryMapper.updateByPrimaryKey(inventory.getPakInventory()) > 0;
    }

    @Override
    public boolean updateSelective(Inventory inventory) {
        Assert.notNull(inventory, "对象不可为空！");
        inventory.setUpdateTime(new Date());
        return pakInventoryMapper.updateByPrimaryKeySelective(inventory.getPakInventory()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Inventory getByPrimaryKey(Long primaryKey) {
        PakInventory pakInventory = pakInventoryMapper.selectByPrimaryKey(primaryKey);
        return null == pakInventory ? null : new Inventory(pakInventory);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Inventory getOneByEntity(Inventory test) {
        Assert.notNull(test, "对象不可为空！");
        PakInventory pakInventory = pakInventoryMapper.selectOne(test.getPakInventory());
        return null == pakInventory ? null : new Inventory(pakInventory);
    }

    @Override
    public List<Inventory> listAll() {
        List<PakInventory> pakInventorys = pakInventoryMapper.selectAll();
        if (CollectionUtils.isEmpty(pakInventorys)) {
            return null;
        }
        List<Inventory> inventorys = new ArrayList<>();
        for (PakInventory pakInventory : pakInventorys) {
            inventorys.add(new Inventory(pakInventory));
        }
        return inventorys;
    }

    @Override
    public List<Inventory> listByEntity(Inventory entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Inventory> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakInventory> pakInventorys = pakInventoryMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakInventorys)) {
            return null;
        }
        List<Inventory> inventorys = new ArrayList<>();
        for (PakInventory pakInventory : pakInventorys) {
            inventorys.add(new Inventory(pakInventory));
        }
        PageInfo bean = new PageInfo<PakInventory>(pakInventorys);
        bean.setList(inventorys);
        return bean;
    }
    @Override
    public void increase(PakInventory pakInventory) {
        pakInventoryMapper.insert(pakInventory);
    }


}
